﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.HealthMonitoring;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
using MedicalPCTerminal.Write.API.Appcation.Commands.HealthMonitoringCommand;

namespace MedicalPCTerminal.Write.API.Appcation.CommandHandlers.HealthMonitoringCommandHandler
{
    /// <summary>
    /// 血压监测
    /// </summary>
    public class AddBloodPressureCommandHandler : IRequestHandler<AddBloodPressureCommand, APIResult<int>>
    {
        /// <summary>
        /// sqlsugar
        /// </summary>
        private readonly SqlSugarContext sugar;
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sugar">sugar</param>
        public AddBloodPressureCommandHandler(SqlSugarContext sugar, IMapper mapper)
        {
            this.sugar = sugar;
            this.mapper = mapper;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<APIResult<int>> Handle(AddBloodPressureCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<int> result = new APIResult<int>();
                var entity = mapper.Map<BloodPressure>(request);
                entity.CreateTime = DateTime.Now;
                var i = sugar.db.Insertable<BloodPressure>(entity).ExecuteCommand();
                if (i > 0)
                {
                    result.Code = APIEnum.添加成功;
                    result.Msg = APIEnum.添加成功.ToString();
                    result.data = i;
                }
                else
                {
                    result.Code = APIEnum.添加失败;
                    result.Msg = APIEnum.添加失败.ToString();
                    result.data = i;
                }
                return await Task.FromResult(result);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
